from django.shortcuts import render,redirect,HttpResponse
from django.views import View
import json
from .models import Course
from rest_framework.views import APIView
from rest_framework.parsers import FormParser,JSONParser
from django.conf import settings
from django.core.serializers import serialize

class LoginView(APIView):
    parser_classes = [FormParser]
    def get(self,request):
        print(settings.NAME)
        return render(request,'login.html')
    '''
    def post(self,request):
        print(type(request))
        origin_data = (request.body).decode('utf-8')
        print(origin_data)
        ret = json.loads(origin_data)
        print("ret:",ret)

        return HttpResponse('ok')
    '''

    def post(self,request):
        data = request.data
        print(data)
        return HttpResponse('ok')

class CourseView(APIView):
    '''
    def get(self,request):
        course_list = list()
        for course in Course.objects.all():
            course = {
                "name":course.name,
                "description":course.descript
            }
            course_list.append(course)

        return HttpResponse(json.dumps(course_list,ensure_ascii=False))
    '''

    def get(self,request):
        origin_data = Course.objects.all()
        # 开始序列化
        serialize_data = serialize("json",origin_data)
        print(serialize_data)
        return HttpResponse(serialize_data)


    def post(self,request):
        pass
